﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using WPNextTools.NEventArgs;

namespace WPNextTools.NBase
{
    public abstract class DisposableObject : SuspendableObject,IDisposable
    {
        #region Fields
        private BitVector32 bitState;
        private const uint IsDisposingStateKey = 1u;
        private const uint IsDisposedStateKey = 2u;
        private EventHandler disposingEvent;
        private EventHandler disposedEvent;
        #endregion

        #region Constructor

        public DisposableObject()
        {
            bitState = new BitVector32(0u);
        }
        
        #endregion

        #region Properties
        public bool IsDisposing
        {
            get
            {
                return this.bitState[IsDisposingStateKey];
            }
        }

        public bool IsDisposed
        {
            get
            {
                return this.bitState[IsDisposedStateKey];
            }
        }

        public event EventHandler Disposing
        {
            add
            {
                this.disposedEvent = (EventHandler)Delegate.Combine(this.disposingEvent, value);
            }
            remove
            {
                this.disposedEvent = (EventHandler)Delegate.Remove(this.disposingEvent, value);
            }
        }

        public event EventHandler Disposed
        {
            add
            {
                this.disposedEvent = (EventHandler)Delegate.Combine(this.disposedEvent, value);
            }
            remove
            {
                this.disposedEvent = (EventHandler)Delegate.Remove(this.disposedEvent, value);
            }
        }

        #endregion

        protected internal bool this[uint key]
        {
            get
            {
                return this.bitState[key];
            }
            set
            {
                this.bitState[key] = value;
            }
        }

        protected internal void SetBitstate(uint key,bool value)
        {
            bool flag = this[key];
            if (flag == value)
            {
                return;
            }
            this.bitState[key] = value;
            this.OnBitStateChanged(new BitStateChangedEventArgs(key, flag, value));
        }

        protected virtual void OnBitStateChanged(BitStateChangedEventArgs e)
        {
            
        }

        protected virtual void CollectEvents(List<Delegate> events)
        {
            events.Add(this.disposingEvent);
            events.Add(this.disposedEvent);
        }

        #region Dispose
        public void Dispose()
        {
            this.DisposeCore(true);
            GC.SuppressFinalize(this);
        }

        private void DisposeCore(bool disposing)
        {
            if (this.IsDisposed)
            {
                return;
            }
            if (this.IsDisposing)
            {
                throw new InvalidOperationException("object is currently disposing its resource");
            }
            this.bitState[IsDisposingStateKey] = true;
            if (disposing)
            {
                this.DisposeManagedResources();
            }
            this.DisposeUnManagedResources();
            this.bitState[IsDisposingStateKey] = false;
            this.bitState[IsDisposedStateKey] = true;
        }
        #endregion
        

        #region DisposeReosources
        /// <summary>
        /// 销毁托管资源
        /// </summary>
        protected virtual void DisposeManagedResources()
        {
            List<Delegate> list = new List<Delegate>();
            this.CollectEvents(list);
            for (int i = 0; i < list.Count; i++)
            {
                Delegate iDelegate = list[i];
                if (iDelegate != null)
                {
                    Delegate[] invocationList = iDelegate.GetInvocationList();
                    for (int j = 0; j < invocationList.Length; j++)
                    {
                        Delegate removeDelgate = invocationList[i];
                        iDelegate = Delegate.Remove(iDelegate, removeDelgate);
                    }
                }
            }
        }
        /// <summary>
        /// 销毁非托管资源
        /// </summary>
        protected virtual void DisposeUnManagedResources()
        {

        }

        #endregion

    }
}
